package vip.idiy.anwei_android_iot.utils;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcelable;

import com.tencent.mmkv.MMKV;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * ClassName SpUtils
 * describe:
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 15:55 2023/4/29
 */
public class SpUtils {
    private static final String TAG = "myTag " + " SpUtils ";

    private static SpUtils mInstance;
    private static MMKV mv;

    private SpUtils() {
        mv = MMKV.defaultMMKV();
    }

    /**
     * 获取SpUtils实例
     *
     * @return SpUtils实例
     */
    public static SpUtils getInstance() {
        if (mInstance == null) {
            synchronized (SpUtils.class) {
                if (mInstance == null) {
                    mInstance = new SpUtils();
                }
            }
        }
        return mInstance;
    }

    /**
     * 迁移旧数据
     * @param context 上下文
     * @param mode 模式
     */
    public static void migrateOldData(Context context, String mode){
        SharedPreferences old_man = context.getSharedPreferences(mode, MODE_PRIVATE);
        mv.importFromSharedPreferences(old_man);
        old_man.edit().clear().commit();
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param key 键
     * @param object 值
     */
    public static void encode(String key, Object object) {
        if (object instanceof String) {
            mv.encode(key, (String) object);
        } else if (object instanceof Integer) {
            mv.encode(key, (Integer) object);
        } else if (object instanceof Boolean) {
            mv.encode(key, (Boolean) object);
        } else if (object instanceof Float) {
            mv.encode(key, (Float) object);
        } else if (object instanceof Long) {
            mv.encode(key, (Long) object);
        } else if (object instanceof Double) {
            mv.encode(key, (Double) object);
        } else if (object instanceof byte[] ) {
            mv.encode(key, (byte[]) object);
        } else {
            mv.encode(key, object.toString());
        }
    }

    /**
     * 保存Set<String>类型的数据
     *
     * @param key 键
     * @param sets 值
     */
    public static void encodeSet(String key, Set<String> sets) {
        mv.encode(key, sets);
    }

    /**
     * 保存Parcelable类型的数据
     *
     * @param key 键
     * @param obj 值
     */
    public static void encodeParcelable(String key, Parcelable obj) {
        mv.encode(key, obj);
    }

    /**
     * 获取int类型的值
     *
     * @param key 键
     * @return int类型的值
     */
    public static Integer decodeInt(String key) {
        return mv.decodeInt(key, 0);
    }

    /**
     * 获取double类型的值
     *
     * @param key 键
     * @return double类型的值
     */
    public static Double decodeDouble(String key) {
        return mv.decodeDouble(key, 0.00);
    }

    /**
     * 获取long类型的值
     *
     * @param key 键
     * @return long类型的值
     */
    public static Long decodeLong(String key) {
        return mv.decodeLong(key, 0L);
    }

    /**
     * 获取boolean类型的值
     *
     * @param key 键
     * @return boolean类型的值
     */
    public static Boolean decodeBoolean(String key) {
        return mv.decodeBool(key, false);
    }

    /**
     * 获取float类型的值
     *
     * @param key 键
     * @return float类型的值
     */
    public static Float decodeFloat(String key) {
        return mv.decodeFloat(key, 0F);
    }

    /**
     * 获取byte[]类型的值
     *
     * @param key 键
     * @return byte[]类型的值
     */
    public static byte[] decodeBytes(String key) {
        return mv.decodeBytes(key);
    }

    /**
     * 获取String类型的值
     *
     * @param key 键
     * @return String类型的值
     */
    public static String decodeString(String key) {
        return mv.decodeString(key,"");
    }

    /**
     * 获取Set<String>类型的值
     *
     * @param key 键
     * @return Set<String>类型的值
     */
    public static Set<String> decodeStringSet(String key) {
        return mv.decodeStringSet(key, Collections.<String>emptySet());
    }

    /**
     * 获取Parcelable类型的值
     *
     * @param key 键
     * @return Parcelable类型的值
     */
    public static Parcelable decodeParcelable(String key) {
        return mv.decodeParcelable(key, null);
    }

    /**
     * 移除某个key对应的值
     *
     * @param key 键
     */
    public static void removeKey(String key) {
        mv.removeValueForKey(key);
    }

    /**
     * 清除所有key
     */
    public static void clearAll() {
        mv.clearAll();
    }

}
